<?php
/* * 注意此文档为UTF-8的编码.
 * Created on 2010-6-29
 *
 * @author EXT <isrn@qq.com>
 *
 */
include ('../../includes/global.php');
include ('../global.php');
include ("../power.php");

/**
 *初始化排序
 * @param type $table
 * @param type $tid
 */
function init_order($table,$tid){

    $result = mysql_query("SELECT id,{$_POST['zd']} FROM `se_" . $table . "` where tid = '" . $tid . "' and " . $_POST['zd'] . " !=0 " . $lgcondition . " order  by " . $_POST['zd'] . "+0 desc");
    if (!$result) {
        die('Cannot view the database: ' . mysql_error().__FILE__.' at line:'.__LINE__);
    } else {
        while ($row = mysql_fetch_assoc($result)) {
            /**
             * 判断是否如始化过
             */
            if($row[$_POST['zd']]>1){
                return ;
            }
            $result_arr[] = $row;
        }
        mysql_free_result($result);
    }
    $px = 1;
    foreach ($result_arr as $value) {
        $gx[$_POST['zd']] = $px;
        Du('sevenel', $table, $gx, 'id', $value['id']);
        $px++;
    }
}

$_REQUEST['lg'] != '' && $lgcondition = " and lg like '" . $_REQUEST['lg'] . "' ";
Tr($_GET['lg'], 'lg');
switch ($_REQUEST['op']) {
    case 'getOrderField': {
            $fields = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "del!=1 and tid = '{$_POST['tid']}' {$lgcondition} and type='textfield' and fname!='{$_POST['orderField']}' and xr=0 order by px",
                'tn' => 'fld'
            ));
            $f[] = "new Ext.grid.CheckboxSelectionModel()";
            $f[] = "{name:'id'},{name:'{$_POST['orderField']}'}";
            $cm[] = "{id:'id',header:'ID',dataIndex:'id',width:30,hidden :false}";
            $cm[] = "{id:'{$_POST['orderField']}',header:'位置（倒序数字越大越靠前）',dataIndex:'{$_POST['orderField']}',width: 168,sortable:true,editor: new fm.TextField({allowBlank: false})}";
            $getField[] = "id,{$_POST['orderField']}";
            foreach($fields as $k => $v){
                $f[] = "{name:'{$v['fname']}'}";
                //隐藏4列以后的信息
                $hidden = (count($cm)>4)?'true':'false';
                $cm[] = "{id:'{$v['fname']}',header:'{$v['name']}',dataIndex:'{$v['fname']}',hidden :{$hidden}}";
                $getField[] = $v['fname'];
                if(count($showField)<4){
                    $showField[] = $v['fname'];
                }
            }
            $R['fields'] = join(',',$f);
            $R['cm'] = join(',',$cm);
            $R['showField'] = join(',',$showField);
            $R['getField'] = join(',',$getField);
            echo json_encode($R);
            exit;
            break;
        }
    case 'show': {
            Tr($_GET['tid'], 'tid');
            Tr($_GET['zd'], 'zd');
            Tr($_GET['lg'], 'lg');
            $tid = explode('@', $_GET['tid']);
            Tr($tid[1], 'tablename2');
            Ts('show1/px.html');
            break;
        }
    case 'showall': {
            $tid = explode('@', $_POST['tid']);
            init_order($tid[1],$tid[0]);
//			print_r($tid);
            Tr($tid[1], 'tablename2');
            $_REQUEST['lg'] != '' && $lgcondition = " and lg like '" . $_REQUEST['lg'] . "' ";
            $bdid_con = $_REQUEST['bdid']!=''?" and bdid='{$_REQUEST['bdid']}'":'';
            $result = mysql_query("SELECT {$_POST['getField']}  FROM `se_" . $tid[1] . "` where del!=1 and tid = '" . $tid[0] . "' and " . $_POST['zd'] . " !=0 {$bdid_con} " . $lgcondition . " order  by " . $_POST['zd'] . "+0 desc");
            if (!$result) {
                die('Cannot view the database: ' . mysql_error().__FILE__.' at line:'.__LINE__);
            } else {
                while ($row = mysql_fetch_assoc($result)) {
                    $result_arr[] = $row;
                }
                mysql_free_result($result);
            }
            echo json_encode($result_arr);

            break; }
    case 'pxcs': {
            ext_request('ok');
            break;
            $tid = explode('@', $_POST['tid']);
            $result = mysql_query("SELECT {$_POST['getField']} FROM `se_" . $tid[1] . "` where tid = '" . $tid[0] . "' and " . $_POST['zd'] . " !=0 " . $lgcondition . " order  by " . $_POST['zd'] . "+0 desc");
            if (!$result) {
                die('Cannot view the database: ' . mysql_error().__FILE__.' at line:'.__LINE__);
            } else {
                while ($row = mysql_fetch_assoc($result)) {
                    $result_arr[] = $row;
                }
                mysql_free_result($result);
            }
            $px = 1;
            foreach ($result_arr as $value) {
                $gx[$_POST['zd']] = $px;
                Du('sevenel', $tid[1], $gx, 'id', $value['id']);
                $px++;
            }
            ext_request('ok');
            break; }
    case 'px': {
            $tid = explode('@', $_POST['tid']);
            $id = $_POST['nowid'];
            $nowfield = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "id = $id ",
                'tn' => $tid[1],
                'limit' => 1
                    ));
            $gx[$_POST['zd']] = $_POST['wz'] + 1;
            $oldfld = Dgd(array(
                'db' => 'd_sevenel',
                'cd' => "tid = '" . $nowfield['tid'] . "' and " . $_POST['zd'] . "='" . $gx[$_POST['zd']] . "'  " . $lgcondition . " ",
                'tn' => $tid[1],
                'limit' => 1
                    ));
            $xx[$_POST['zd']] = $nowfield[$_POST['zd']];
//			Du('sevenel','fld',$gx,'id',$id);
            if ($xx[$_POST['zd']] > $_POST['wz']) {
                $d_sevenel->Desql("update  `se_" . $tid[1] . "` set " . $_POST['zd'] . "=" . $_POST['zd'] . "+1 WHERE 1 and tid = '" . $nowfield['tid'] . "' and " . $_POST['zd'] . ">='" . $gx[$_POST['zd']] . "' and " . $_POST['zd'] . " <='" . $xx[$_POST['zd']] . "'   " . $lgcondition . "");
                Du('sevenel', $tid[1], $gx, 'id', $id);
            } else {
                $d_sevenel->Desql("update  `se_" . $tid[1] . "` set " . $_POST['zd'] . "=" . $_POST['zd'] . "-1 WHERE 1 and tid = '" . $nowfield['tid'] . "' and " . $_POST['zd'] . ">='" . $xx[$_POST['zd']] . "' and " . $_POST['zd'] . " <='" . $gx[$_POST['zd']] . "'  " . $lgcondition . "");
                Du('sevenel', $tid[1], $gx, 'id', $id);
            }
            ext_request('ok');
            break;
        }
    case 'del': {
            $_POST['ids'] = json_decode(stripslashes($_POST['ids']));
            $tid = explode('@', $_POST['tid']);
            $gx[$_POST['zd']] = 0;
            if (is_array($_POST['ids'])) {
                foreach ($_POST['ids'] as $id) {
                    Du('sevenel', $tid[1], $gx, 'id', $id);
                }
            } else {
                Du('sevenel', $tid[1], $gx, 'id', $_POST['ids']);
            }
            echo 'ok';
            break;
        }
}
?>